package xyz.wadewhy.basicstest.easyExcel.test;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.ExcelWriter;
import com.alibaba.excel.util.ListUtils;
import com.alibaba.excel.write.metadata.WriteSheet;
import com.alibaba.excel.write.metadata.WriteTable;
import com.alibaba.excel.write.style.column.LongestMatchColumnWidthStyleStrategy;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.apache.poi.ss.formula.functions.T;
import xyz.wadewhy.basicstest.easyExcel.color.CellRowHeightStyleStrategyx;
import xyz.wadewhy.basicstest.easyExcel.color.CellRowWightStyleStrategy;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

public class ExcelTest2 {
    public static void main(String[] args) {
        repeatedWrite();
    }

    public static void test1() {
        // 方法1 如果写到同一个sheet
        String fileName = "repeatedWrite" + System.currentTimeMillis() + ".xlsx";
        ExcelWriter excelWriter = null;
        try {
            // 这里 需要指定写用哪个class去写
            excelWriter = EasyExcel.write(fileName, DemoData.class).build();
            // 这里注意 如果同一个sheet只要创建一次
            WriteSheet writeSheet = EasyExcel.writerSheet("模板").build();
            // 去调用写入,这里我调用了五次，实际使用时根据数据库分页的总的页数来
            for (int i = 0; i < 5; i++) {
                // 分页去数据库查询数据 这里可以去数据库查询每一页的数据
                List<DemoData> data = data();
                excelWriter.write(data, writeSheet);
            }
        } finally {
            // 千万别忘记finish 会帮忙关闭流
            if (excelWriter != null) {
                excelWriter.finish();
            }
        }
    }


    public static void test3() {

        // 方法3 如果写到不同的sheet 不同的对象
        String fileName = "repeatedWrite" + System.currentTimeMillis() + ".xlsx";
        ExcelWriter excelWriter = null;
        try {
            // 这里 指定文件
            excelWriter = EasyExcel.write(fileName).build();
            // 去调用写入,这里我调用了五次，实际使用时根据数据库分页的总的页数来。这里最终会写到5个sheet里面
            for (int i = 0; i < 5; i++) {
                // 每次都要创建writeSheet 这里注意必须指定sheetNo 而且sheetName必须不一样。这里注意DemoData.class 可以每次都变，我这里为了方便 所以用的同一个class 实际上可以一直变
                WriteSheet writeSheet = EasyExcel.writerSheet(i, "模板" + i).head(DemoData.class).build();
                // 分页去数据库查询数据 这里可以去数据库查询每一页的数据
                List<DemoData> data = data();
                excelWriter.write(data, writeSheet);
            }
        } finally {
            // 千万别忘记finish 会帮忙关闭流
            if (excelWriter != null) {
                excelWriter.finish();
            }
        }
    }

    /**
     * 重复多次写入
     * <p>
     * <p>
     * <p>
     * 3. 直接调用二次写入即可
     */
    public static void repeatedWrite() {

        test2();

    }

    private static List<DemoData> data() {
        List<DemoData> list = ListUtils.newArrayList();
        for (int i = 0; i < 10; i++) {
            DemoData data = new DemoData();
            data.setString("字符串" + i);
            data.setDate(new Date());
            data.setDoubleData(0.56);
            list.add(data);
        }
        return list;
    }

    private static List<DemoData2> data2() {
        List<DemoData2> list = ListUtils.newArrayList();
        for (int i = 0; i < 10; i++) {
            DemoData2 data = new DemoData2();
            data.setName("wadewhy" + i);
            data.setAge(10 + i);
            list.add(data);
        }
        return list;
    }

    public static void test2() {
        // 方法2 如果写到不同的sheet 不同一个对象
        String fileName = "repeatedWrite" + System.currentTimeMillis() + ".xlsx";
        ExcelWriter excelWriter = null;
        Student str = buildStudent();
        try {
            List<Integer> columnIndexes = Arrays.asList(0, 1, 2);
            //自定义标题和内容策略(具体定义在下文)
//            CellStyleStrategy cellStyleStrategy =
//                    new CellStyleStrategy(columnIndexes, new WriteCellStyle(), new WriteCellStyle());

            // 这里 指定文件
//            excelWriter = EasyExcel.write(fileName).build();
            excelWriter = EasyExcel.write(fileName, DemoData.class).registerWriteHandler(new CellRowHeightStyleStrategyx())   //设置行高的策略
                    .registerWriteHandler(new CellRowWightStyleStrategy())        //设置表头和内容的策略
                    .build();
            // 去调用写入,这里我调用了五次，实际使用时根据数据库分页的总的页数来。这里最终会写到5个sheet里面
            for (int i = 0; i < str.sheets.size(); i++) {
                WriteSheet writeSheet = null;
                if (str.sheets.get(i).type == 0) {
                    writeSheet =  EasyExcel.writerSheet(i, str.sheets.get(i).sheetName).head(DemoData.class).build();
                } else {
                    writeSheet =  EasyExcel.writerSheet(i, str.sheets.get(i).sheetName).head(DemoData2.class).build();
                }
                // 分页去数据库查询数据 这里可以去数据库查询每一页的数据
//                excelWriter.write(str.sheets.get(i).data, writeSheet);

                // 这里必须指定需要头，table 会继承sheet的配置，sheet配置了不需要，table 默认也是不需要
                WriteTable writeTable0 = EasyExcel.writerTable(0).registerWriteHandler(new LongestMatchColumnWidthStyleStrategy()).needHead(Boolean.TRUE).automaticMergeHead(true).head(head4()).build();
                WriteTable writeTable1 = EasyExcel.writerTable(1).needHead(Boolean.TRUE).build();
                excelWriter.write(data(), writeSheet, writeTable0);
                // 第二次写如也会创建头，然后在第一次的后面写入数据
                writeTable1.setHead(head1());
                excelWriter.write(data(), writeSheet, writeTable1);
                WriteTable writeTable2 = EasyExcel.writerTable(2).needHead(Boolean.TRUE).build();
                // 第二次写如也会创建头，然后在第一次的后面写入数据
                writeTable2.setHead(head1());
                excelWriter.write(data(), writeSheet, writeTable2);
            }
        } finally {
            // 千万别忘记finish 会帮忙关闭流
            if (excelWriter != null) {
                excelWriter.finish();
            }
        }
    }

    private static List<List<String>> head4() {
        TestUtil testUtil = new TestUtil();
        List<List<String>> sstr = testUtil.head("广州班优小学 学生评比sadagwdgasdghasgdhsadgawdhgashdghasgdhasghdg", "一年级全年级", 3);
        return sstr;
    }
    private static List<List<String>> head1() {
        List<List<String>> list = new ArrayList<List<String>>();
        List<String> head0 = new ArrayList<String>();
        head0.add("全年级");
        head0.add("思想-主动");
        List<String> head1 = new ArrayList<String>();
        head1.add("全年级");
        head1.add("思想-打人");
        List<String> head2 = new ArrayList<String>();
        head2.add("全年级");
        head2.add("思想-的点点滴滴");
        list.add(head0);
        list.add(head1);
        list.add(head2);
        return list;
    }
    private static Student buildStudent() {
        Student stu = new Student();
        List<SheetDemo<T>> sheetDemos = new ArrayList<SheetDemo<T>>();
        SheetDemo sheet = new SheetDemo();
        sheet.setData(data());
        sheet.setSheetName("美国");
        sheet.setType(0);
        sheetDemos.add(sheet);
        SheetDemo sheet2 = new SheetDemo();
        sheet2.setData(data2());
        sheet2.setSheetName("中国");
        sheet2.setType(1);
        sheetDemos.add(sheet2);
        stu.setSheets(sheetDemos);
        return stu;
    }
}

@Data
@AllArgsConstructor
@NoArgsConstructor
class Student {
    List<SheetDemo<T>> sheets = new ArrayList<>();
}

@Data
@AllArgsConstructor
@NoArgsConstructor
class SheetDemo<T> {
    //data
    List<T> data = new ArrayList<T>();
    //sheet
    String sheetName = "";

    int type = 0;
}
